package collectionFrame;

import com.sun.org.apache.xml.internal.utils.res.IntArrayWrapper;

import java.util.*;

public class Demo4_CollectionsArithmetic {


    public static void userCollections(List<Integer> list){
        Collections.sort(list);
        System.out.println(list);

        Collections.shuffle(list);
        System.out.println(list);

        System.out.println(Collections.max(list));

        //目标列表的实际长度一定要大于等于原列表的实际长度才可以复制","就是覆盖
        ArrayList<Integer> listNew = new ArrayList<>();
        listNew.add(9);
        listNew.add(8);
        listNew.add(7);
        listNew.add(6);
        listNew.add(5);
        listNew.add(4);
        System.out.println(listNew.size());
        System.out.println(list.size());

        Collections.copy(listNew,list);
        System.out.println(listNew);

        Collections.reverse(list);
        System.out.println(list);
    }


    public static void useArrays(int[] array){

        //把不定长个某个类型的元素转成这个类型的列表","不能直接把数组转成一个列表
        List<Integer> ints = Arrays.asList(2,3,4,5,6,7);
        System.out.println(ints);


        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.toString(Arrays.copyOf(array, 20)));

        Arrays.fill(array,100);
        System.out.println(Arrays.toString(array));
    }

    public static void compareTest(){
//        List<Integer> list_nums = new ArrayList<>();
//        list_nums.add(5);
//        list_nums.add(9);
//        list_nums.add(4);
//        list_nums.add(7);
//        list_nums.add(1);
//        Collections.sort(list_nums);
//        System.out.println(list_nums);

//        List<String> list_str = new ArrayList<>();
//        list_str.add("hello");
//        list_str.add("heawe");
//        list_str.add("banana");
//        list_str.add("apple");
//        list_str.add("orange");
//        Collections.sort(list_str);
//        System.out.println(list_str);

        List<Student> list_stu = new ArrayList<>();
        list_stu.add(new Student(1,"z张天琪"));
        list_stu.add(new Student(7,"h赵胜军"));
        list_stu.add(new Student(4,"h赵胜军"));
        list_stu.add(new Student(6,"h赵胜军"));
        list_stu.add(new Student(3,"w开橡皮"));
        list_stu.add(new Student(5,"s徐豪"));
        list_stu.add(new Student(2,"m马宏泽"));
        Collections.sort(list_stu);
//        Collections.sort(list_stu,new ChangeStudentSortRule());
        System.out.println(list_stu);

    }
    
    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(100);
        integers.add(6);
        integers.add(94);
        integers.add(72);
        Demo4_CollectionsArithmetic.userCollections(integers);

        int[] nums = {2,6,4,8,9,4,5,3};
        Demo4_CollectionsArithmetic.useArrays(nums);

        Demo4_CollectionsArithmetic.compareTest();

    }


}

class Student implements Comparable<Student>{
    private int stuId;
    private String stuName;

    public Student(int stuId, String stuName) {
        this.stuId = stuId;
        this.stuName = stuName;
    }

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int thatId = o.getStuId();
        int thisId = this.stuId;

        String thisName = this.stuName;
        String thatName = o.getStuName();

        //如果姓名相同","按照学号升序排序
        if(thisName.equals(thatName)){
            return thisId-thatId;
        }else{
            //如果姓名不同","按照姓名倒序排序
            return -(thisName.compareTo(thatName));
        }
    }
}

/**
 * 自定义 Student 类型的排序规则
 */
class ChangeStudentSortRule implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        int thisId = o1.getStuId();
        int thatId = o2.getStuId();

        String thisName = o1.getStuName();
        String thatName = o2.getStuName();

        //如果姓名相同","按照id降序排列
        if (thisName.equals(thatName)) {
            return thatId - thisId;
        } else {
            //如果姓名不同","按姓名升序排列
            return thisName.compareTo(thatName);
        }
    }
}
